<?php
/**
 * 文章基础模型model
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/19
 * Time: 17:43
 */
namespace Home\Model;
use Think\Model;
class EcmsNewsModel extends Model
{
    /**
     * 通过新闻id获取新闻详情信息
     * @param $id
     * @param string $fields
     * @return array|mixed
     */
    public function getNewById($id,$fields = '*')
    {
        $data = $this -> field($fields) -> where('id = '.$id) -> find();
        return $data ? $data : [];
    }

    /**
     * 通过父类id获取文章
     * @param $classId
     * @param string $fields
     * @param int $limit
     * @return array|mixed
     */
    public function getNewsByClassId($classId,$fields = '*',$limit = 1)
    {
        $temp = $this -> field($fields) -> where('classid = '.$classId.' and isgood <> 0');
        if( !is_null($limit) )
        {
            $temp = $temp -> limit($limit);
        }
        $data = $temp -> select();

        return $data ? $data : [];
    }

    /**
     * 获取文章信息，通过传递的id字符串
     * @param $classStr
     * @param string $fields
     * @param string $order
     * @param int $limit
     * @return array|mixed
     */
    public function getNewsByClassIdStr($classStr,$fields = '*',$limit = 1, $order = '')
    {
        $condition = [
            'classid' => ['in',$classStr],
        ];
        $temp = $this -> field($fields) -> where($condition);

        if( !is_null($limit) )
        {
            $temp = $temp -> limit($limit);
        }
        if( !is_null($order) )
        {
            $temp = $temp -> order($order);
        }
        $data = $temp -> select();

        return $data ? $data : [];
    }

    /**
     * 随机推荐阅读，暂时简单返回
     * @param $classStr
     * @return array
     * create by wenQing
     */
    public function getTuiJianNews($classStr)
    {
        $condition = [
            'classid' => ['in',$classStr],
        ];
        $data = $this -> field('id,title,titleurl') -> where($condition)->order('rand()')->limit(3)->select();

        return $data ? $data : [];
    }

    /**
     * 文章首页 按照更新时间排序 获取文章数据
     * @param string $fields
     * @param int $limit
     * @param string $order
     * @return array|mixed
     */
    public function getNewsOrderByTime($fields = '*',$limit = 1, $order = ''){

        $condition['classid'] = ['NOTIN','276,277,278,279,280,281,282,283,284,285']; //暂时添加不调用植发站的文章

        $temp = $this -> field($fields)->where($condition);

        if( !is_null($limit) )
        {
            $temp = $temp ->limit($limit);
        }
        if( !is_null($order) )
        {
            $temp = $temp -> order($order);
        }
        $data = $temp -> select();

        return $data ? $data : [];
    }

    /**
     * 获取所需要的新闻数据
     * @param $condition
     * @param string $fields
     * @param string $limit
     * @param string $order
     * @return array
     */
    public function getSelNews($condition,$fields = '*',$limit = '',$order = '')
    {
        $tmp = $this->field($fields)->where($condition);
        
        if(!is_null($limit))
        {
            $tmp = $tmp -> limit($limit);
        }
        if(!is_null($order))
        {
            $tmp = $tmp -> order($order);
        }
        $data = $tmp -> select();
        $count = $this->field('id')->where($condition)->count();

        if($data && $count)
        {
            return [
                'status' => 1,
                'data' => $data ? $data : [],
                'count' => $count ? $count : 0
            ];
        }
        else
        {
            return [
                'status' => 0,
                'data' => []
            ];
        }
    }
}